package com.atguigu.lease.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

//jackson使用Result对象 转json字符串 需要使用getter方法
@Data
@Schema(description = "统一响应类")
public class Result<T> {
    // 浏览器解析响应报文状态码为200时才会解析响应体内容
    @Schema(description = "业务状态码: 0表示成功")
    private Integer code;
    private String message;
    private Boolean ok;
    private T data;
    private Result(){}
    //1 创建Result对象的方法
    public static <T> Result<T> build(Integer code , String message , Boolean ok , T data){
        Result<T> result = new Result<>();
        result.code = code;
        result.message = message;
        result.ok = ok;
        result.data = data;
        return result;
    }
    public static <T> Result<T> build(ResultCodeEnum codeEnum , Boolean ok , T data){
        return build(codeEnum.getCode(), codeEnum.getMessage(),  ok ,data);
    }
    //2 成功方法
    public static <T> Result<T> ok(T data){
        return build(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), true , data);
    }
    public static <T> Result<T> ok(){
        return build(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(),true , null);
    }
    //3 失败方法
    // 失败的情况比较多,一般使用一个状态码描述一种失败的原因
    // 可以使用状态码映射message  ,方便以后排查问题
    /*
        SUCCESS(200, "成功"),
        FAIL(201, "失败"),
        PARAM_ERROR(202, "参数不正确"),
        SERVICE_ERROR(203, "服务异常"),
        DATA_ERROR(204, "数据异常"),
        ILLEGAL_REQUEST(205, "非法请求"),
        REPEAT_SUBMIT(206, "重复提交"),
        DELETE_ERROR(207, "请先删除子集"),

        ADMIN_ACCOUNT_EXIST_ERROR(301, "账号已存在"),
        ADMIN_CAPTCHA_CODE_ERROR(302, "验证码错误"),
        ADMIN_CAPTCHA_CODE_EXPIRED(303, "验证码已过期"),
        ADMIN_CAPTCHA_CODE_NOT_FOUND(304, "未输入验证码"),

     */
    public static <T> Result<T> fail(Integer code , String message){
        return build(code,message,false , null);
    }
    public static <T> Result<T> fail(){
        return fail(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getMessage());
    }
    //使用枚举类对象创建Result对象
    public static <T> Result<T> fail(ResultCodeEnum codeEnum){
        return build(codeEnum.getCode(),codeEnum.getMessage(),false , null);
    }

    public static <T> Result<T> build(boolean b) {
        //根据布尔类型的值创建默认的成功或者失败对象返回
        return b?Result.ok():Result.fail();
    }

    //4 修改属性值链式调用的方法
    public Result<T> code(Integer code){
        this.code = code;
        return this;
    }
    public Result<T> message(String message){
        this.message = message;
        return this;
    }
    public Result<T> ok(Boolean ok){
        this.ok = ok;
        return this;
    }
    public Result<T> data(T data){
        this.data = data;
        return this;
    }
}
